package com.ebelter.btcomlib.models.https;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.models.https.responses.GetBloodPressureData1;
import com.ebelter.btcomlib.models.https.responses.GetBloodPressureData2;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData2;
import com.ebelter.btcomlib.models.https.responses.LoginResponse;
import com.ebelter.btcomlib.models.https.responses.RegisterResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


public class NetUtils {
    private static final String TAG = "NetUtils";
    private static NetUtils instance;
    private BaseJsonGenerator mBaseJsonGenerator;

    private NetUtils(){
        mBaseJsonGenerator=new BaseJsonGenerator();
    }
    public static NetUtils getInstance() {
        if (instance==null){
            synchronized (NetUtils.class){
                if (instance==null) instance = new NetUtils();
            }
        }
        return instance;
    }


    public static boolean available() {

        ConnectivityManager connectivity = (ConnectivityManager) CommonLib.APPContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo anInfo : info) {
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        if (anInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            return true;
                        } else if (anInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }
    /**
     * get请求获取数据
     */
    public void get(Object tag, String url,StringCallback callback){
        OkGo.get(url).tag(tag).execute(callback);
    }

    public void getFileDownLoad(Object tag, String url, FileCallback fileCallback){
        OkGo.get(url).tag(tag).execute(fileCallback);
    }


    public void cancelTag(Object tag){
        OkGo.getInstance().cancelTag(tag);
    }
    /**
     * post请求获取数据
     */
    public void post(Object tag, String url, StringCallback callback){
        post(tag,url,null,callback);
    }
    /**
     * post请求获取数据
     */
    public  void post(Object tag, String url, HttpParams httpParams, StringCallback callback){
        OkGo.post(url).tag(tag).params(httpParams).execute(callback);
    }

    /**
     * postString请求获取数据
     */
    public void postString(Object tag, String url, String requestString, StringCallback callback){
        OkGo.post(url).tag(tag).upString(requestString).execute(callback);
    }
    /**
     * postJson请求获取数据
     */
    public void postJson(Object tag, String url, String requestJson, StringCallback callback){
        OkGo.post(url).tag(tag).upJson(requestJson).execute(callback);
    }

    //-----------------------------------------------------------------------------------------------------------------------------
    static final String  R_S_R_S = "请求成功,解析成功";
    static final String  R_S_R_F = "请求成功，解析失败";
    static final String  R_F = "请求失败";


    /**获取邮箱验证码 type=1 注册 type=2 忘记密码*/
    public void getEmailValidateCodeRequest(Object tag, String email, int type, final HttpResponse<getEmailValidateCodeResponse> httpResponse){
        postJson(tag, HttpsUrl.getEmailValidateCodeUrl, mBaseJsonGenerator.getEmailValidateRJ(email, type, AppUtils.getLocalStr()), new MyStringCallback(httpResponse,getEmailValidateCodeResponse.class));
    }
    /**获取邮箱验证码 type=1 注册 type=2 忘记密码*/
    public void getPhoneValidateCodeRequest(Object tag, String phone, int type, final HttpResponse<getEmailValidateCodeResponse> httpResponse){
        postJson(tag, HttpsUrl.getPhoneValidateCodeUrl, mBaseJsonGenerator.getPhoneValidateRJ(phone, type, AppUtils.getLocalStr()), new MyStringCallback(httpResponse,getEmailValidateCodeResponse.class));
    }


    /**注册*/
    public void registerUser(Object tag,Map<String, Serializable> paramMap, final HttpResponse<RegisterResponse> httpResponse){
        String registerjson=mBaseJsonGenerator.getMapRJ(paramMap);
        LogUtils.i(TAG,"注册Json："+registerjson);
        postJson(tag, HttpsUrl.registerUrl, registerjson, new MyStringCallback(httpResponse,RegisterResponse.class));

    }

    /**注册*/
    public void registerUser3(Object tag,Map<String, Serializable> paramMap, final HttpResponse<RegisterResponse> httpResponse){
        String registerjson=mBaseJsonGenerator.getMapRJ(paramMap);
        LogUtils.i(TAG,"注册Json："+registerjson);
        postJson(tag, HttpsUrl.registerUrl3, registerjson, new MyStringCallback(httpResponse,RegisterResponse.class));

    }

//    Map<String, Serializable> paramMap = new HashMap<>();
//        paramMap.put(IHttpRequestField.nickName,nickName);
//        paramMap.put(IHttpRequestField.password, MD5Utils.getMd5(psw));
    //登录
    public void loginRequest(Object tag,String nickname,String psw, final HttpResponse<LoginResponse> httpResponse) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(IHttpRequestField.nickName,nickname);
        paramMap.put(IHttpRequestField.password, MD5Utils.getMd5(psw));
        String json=mBaseJsonGenerator.getMapRJ(paramMap);

       // LogUtils.i(TAG,"登录Json："+json);
        postJson(tag, HttpsUrl.loginUrl, json, new MyStringCallback(httpResponse,LoginResponse.class));
    }

    //找回密码
    public void findPsw(Object tag, String email, String newPassword, String emailCode, HttpResponse<BaseResponse> httpResponse){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.email,email);
        paramMap.put(IHttpRequestField.newPassword,MD5Utils.getMd5(newPassword));
        paramMap.put(IHttpRequestField.code,emailCode);
        String json=mBaseJsonGenerator.getMapRJ(paramMap);
        //LogUtils.i(TAG,"Json："+json);
        postJson(tag, HttpsUrl.updatePasswordUrl, json, new MyStringCallback(httpResponse,BaseResponse.class));
    }
    //找回密码
    public void findPswPhone(Object tag, String phone, String newPassword, String phoneCode, HttpResponse<BaseResponse> httpResponse){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.mobilePhone,phone);
        paramMap.put(IHttpRequestField.newPassword,MD5Utils.getMd5(newPassword));
        paramMap.put(IHttpRequestField.code,phoneCode);
        String json=mBaseJsonGenerator.getMapRJ(paramMap);
        //LogUtils.i(TAG,"Json："+json);
        postJson(tag, HttpsUrl.updatePasswordUrl_phone, json, new MyStringCallback(httpResponse,BaseResponse.class));
    }



    //更新用户信息
    public void updateInfo(Object tag,Map<String, Serializable> paramMap, HttpResponse<BaseResponse> httpResponse){
        String json=mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.updateInfoUrl, json, new MyStringCallback(httpResponse,BaseResponse.class));
    }


    /**
     * 上传血压数据到后台
     * @param systolic 收缩压
     * @param diastolic 舒张压
     * @param pulse 脉搏
     * @param testDate 测量时间
     */
    public void uploadBloodPressureDate(Object tag,int systolic,int diastolic,int pulse,String testDate, HttpResponse<BaseResponse> httpResponse){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.systolic,systolic);
        paramMap.put(IHttpRequestField.diastolic,diastolic);
        paramMap.put(IHttpRequestField.pulse,pulse);
        paramMap.put(IHttpRequestField.testDate,testDate);
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.upLoadBloodPressDatas, json, new MyStringCallback(httpResponse,BaseResponse.class));
    }



    public void getTodayBloodPressureData(Object tag,int pageSize,String startTime,String endTime, HttpResponse<GetBloodPressureData1> httpResponse){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.startIndex,0);
        paramMap.put(IHttpRequestField.pageSize,pageSize);
        paramMap.put(IHttpRequestField.startTime,startTime);
        paramMap.put(IHttpRequestField.endTime,endTime);
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        LogUtils.i(TAG,"--getTodayBloodPressureData--json="+json);
        postJson(tag, HttpsUrl.getBloodPressDatas1, json, new MyStringCallback(httpResponse,GetBloodPressureData1.class));
    }

    /**
     * 分页获取历史数据
     * @param startIndex
     * @param pageSize
     * @param httpResponse
     */
    public void getBloodPressureData1(Object tag,int startIndex,int pageSize, HttpResponse<GetBloodPressureData1> httpResponse){

        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.startIndex,startIndex);
        paramMap.put(IHttpRequestField.pageSize,pageSize);
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.getBloodPressDatas1, json, new MyStringCallback(httpResponse,GetBloodPressureData1.class));
    }

    public void getBloodPressureData1(Object tag,Map<String,Serializable> paramMap, HttpResponse<GetBloodPressureData1> httpResponse){
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.getBloodPressDatas1, json, new MyStringCallback(httpResponse,GetBloodPressureData1.class));
    }

    public void getTempertureData1(Object tag,Map<String,Serializable> paramMap, HttpResponse<GetTempertureData> httpResponse){
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.getTempertureDatas, json, new MyStringCallback(httpResponse,GetTempertureData.class));
    }
    /**
     * 分页获取历史数据
     * @param startIndex
     * @param pageSize
     * @param httpResponse
     */
    public void getBloodPressureData2(Object tag,int startIndex,int pageSize,String startTime,String endTime, HttpResponse<GetBloodPressureData2> httpResponse){

        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.startIndex,startIndex);
        paramMap.put(IHttpRequestField.pageSize,pageSize);
        paramMap.put(IHttpRequestField.startTime,startTime);
        paramMap.put(IHttpRequestField.endTime,endTime);
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.getBloodPressDatas2, json, new MyStringCallback(httpResponse,GetBloodPressureData2.class));
    }


    public void getData_T(Object tag,String testDate,HttpResponse<GetTempertureData2> httpResponse){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.testDate,testDate);
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        LogUtils.i(TAG,"--getData_T--json="+json);
        postJson(tag, HttpsUrl.getTempertureDatas2, json, new MyStringCallback(httpResponse,GetTempertureData2.class));
    }



    //上传体温计的测量数据
    public void uploadMesureData_T(Object tag,Map<String,Serializable> paramMap,HttpResponse<BaseResponse> httpResponse){
        String json = mBaseJsonGenerator.getMapRJ(paramMap);
        postJson(tag, HttpsUrl.upLoadTempertureDatas, json, new MyStringCallback(httpResponse,BaseResponse.class));
    }

    /**
     * 没有连接网络
     */
    public static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    public static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    public static final int NETWORK_WIFI = 1;

    public static int getNetWorkState(Context context) {
        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}
